//
//  ZCRecommendAttentionController.m
//  百思不得姐
//
//  Created by Ning Xie on 16/8/24.
//  Copyright © 2016年 zhangchong. All rights reserved.
//

#import "ZCRecommendAttentionController.h"
#import "ZCAttentionListModel.h"
#import "ZCAttentionContentModel.h"
#import "ZCAttentionListCell.h"
#import "ZCAttentionContentCell.h"


@interface ZCRecommendAttentionController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak) UITableView *listView;          //左侧列表视图
@property (nonatomic, weak) UITableView *contentView;       //右侧列表视图

@property (strong, nonatomic) NSArray *listArray;           //左侧列表数据
//@property (strong, nonatomic) NSMutableArray *contentArray; //右侧列表数据



@end

@implementation ZCRecommendAttentionController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupUI];
    [self setupHeaderFooterView];
    [self listViewLoadData];
    

}

/**
 *  页面初始化
 */
-(void)setupUI{
    
    UITableView *listView = [[UITableView alloc]init];
    self.listView = listView;
    listView.backgroundColor = ZCGlobalRBG;
    listView.delegate = self;
    listView.dataSource = self;
    listView.bounces = NO;
    listView.rowHeight = 40;
    listView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    UITableView *contentView = [[UITableView alloc]init];
    self.contentView = contentView;
    contentView.dataSource = self;
    contentView.delegate = self;
    contentView.rowHeight = 60;
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.showsVerticalScrollIndicator = NO;
    contentView.showsHorizontalScrollIndicator = NO;
    contentView.separatorStyle = UITableViewCellSeparatorStyleNone;


    [self.view addSubview:listView];
    [self.view addSubview:contentView];
    
    [listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view);
        make.top.equalTo(self.view);
        make.bottom.equalTo(self.view);
        make.width.mas_equalTo(70);
        
    }];
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.bottom.equalTo(self.view);
        make.top.equalTo(self.mas_topLayoutGuide);
        make.leading.mas_equalTo(listView.mas_trailing);
    }];
}

/**
 *  加载上拉刷新控件
 */
-(void)setupHeaderFooterView{
    
    self.contentView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(contentViewLoadMoreData)];
    self.contentView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(contentViewLoadNewData)];
    self.contentView.mj_footer.hidden = YES;
}

/**
 *  左侧列表加载数据
 */
-(void)listViewLoadData{
    
    [[ZCNetworkingTools sharedNetWorkingTools] requestAttentionList:^(NSDictionary *response, NSError *error) {
        
        if (error != nil) {
            ZCLog(@"数据请求错误 %@",error);
            [SVProgressHUD showErrorWithStatus:@"数据加载失败"];
            return ;
        }
        if (![response isKindOfClass:[NSDictionary class]]) {
            ZCLog(@"不是一个合法的字典");
            [SVProgressHUD showErrorWithStatus:@"数据加载失败"];
            return;
        }
        
        self.listArray = [ZCAttentionListModel mj_objectArrayWithKeyValuesArray:response[@"list"]];
        
        [self.listView reloadData];
        
        //默认将第一组选中
        [self.listView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
        
        /**
         *  列表刷新完毕之后,刷新右侧列表数据
         */
//      [self.contentView.mj_header beginRefreshing];
        [self contentViewLoadNewData];
    }];

}

/**
 *  右侧列表加载数据
 *
 *  @param model 左侧列表模型
 */
-(void)contentViewLoadDataWithModel:(ZCAttentionListModel*)model{
    
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    
    /**
     *  目的:为了解决重复请求数据
     */
    
    if (model.contentArray.count >0) {
        
        [self.contentView reloadData];
        [self.contentView.mj_header endRefreshing];
        [SVProgressHUD dismiss];

    }else{
        /**
         *  此举目的在于,杜绝上一次页面数据残留
         */
        [self.contentView reloadData];
        
        //右侧列表获取数据
        [self requestAttentionContentWithModel:model page:1];
        
    }

}

/**
 *  右侧列表加载更多数据
 */
-(void)contentViewLoadMoreData {
    
//    if (self.contentView.mj_footer.isRefreshing) {
//        return;
//    }
    
    if (self.contentView.mj_header.isRefreshing) {
        
        [self.contentView.mj_header endRefreshing];
    }
    
    ZCLog(@"加载更多数据");
    ZCAttentionListModel *model = self.listArray[self.listView.indexPathForSelectedRow.row];
    [self requestAttentionContentWithModel:model page:model.next_page];
    
}

/**
 *  右侧列表刷新数据
 */
-(void)contentViewLoadNewData{
    
    if (self.contentView.mj_footer.isRefreshing) {
        [self.contentView.mj_footer endRefreshing];
    }
    ZCAttentionListModel *model = self.listArray[self.listView.indexPathForSelectedRow.row];
    
    [self contentViewLoadDataWithModel:model];
}


/**
 *  右侧列表获取数据封装方法
 *
 *  @param model 模型数据
 *  @param page  页数
 */
-(void)requestAttentionContentWithModel:(ZCAttentionListModel*)model page:(NSInteger)page{

        [[ZCNetworkingTools sharedNetWorkingTools] requestAttentionContentWithCategoryID:[model id] page:page callBack:^(NSDictionary *response, NSError *error) {
            
            if (error != nil) {
                ZCLog(@"数据请求错误 %@",error);
                [SVProgressHUD showErrorWithStatus:@"数据加载失败"];
                [self.contentView.mj_header endRefreshing];
                return ;
            }
            if (![response isKindOfClass:[NSDictionary class]]) {
                ZCLog(@"不是一个合法的字典");
                [SVProgressHUD showErrorWithStatus:@"数据加载失败"];
                [self.contentView.mj_header endRefreshing];
                return;
            }
            [SVProgressHUD dismiss];
            NSArray * tempArray = [ZCAttentionContentModel mj_objectArrayWithKeyValuesArray:response[@"list"]];
            
            model.total_page = [response[@"total_page"] integerValue];
            model.next_page = [response[@"next_page"] integerValue];
            model.total = [response[@"total"] integerValue];
            
            
            /**
             *  将获取的数组添加到模型中
             */
            [model.contentArray addObjectsFromArray:tempArray];
            
            [self.contentView reloadData];
            
            [self.contentView.mj_footer endRefreshing];
            [self.contentView.mj_header endRefreshing];
            
            if (model.next_page > model.total_page) {
                [self.contentView.mj_footer endRefreshingWithNoMoreData];
            }
        }];

    
    

}


//MARK:TableView的数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView == self.listView) {
        
        return self.listArray.count;
        
    }else{
        
        ZCAttentionListModel *model = self.listArray[self.listView.indexPathForSelectedRow.row];
        NSInteger count = model.contentArray.count;
        
        self.contentView.mj_footer.hidden = (count == 0);
        
//        if (count == model.total) {
//            [self.contentView.mj_footer endRefreshingWithNoMoreData];
//        }else{
//            [self.contentView.mj_footer endRefreshing];
//        }
        
        return count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.listView) {
        
        ZCAttentionListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"listCell"];
        
        if (cell == nil) {
            cell = [[ZCAttentionListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"listCell"];
        }
        cell.model = self.listArray[indexPath.row];
        
        return  cell;
        
    }else{
        
        ZCAttentionContentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"contentCell"];
        
        if (cell == nil) {
            cell = [[ZCAttentionContentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"contentCell"];
        }
        ZCAttentionListModel *model = self.listArray[self.listView.indexPathForSelectedRow.row];
        
        cell.model = model.contentArray[indexPath.row];
        
        return  cell;
    }
}

//MARK:TableView的代理方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (tableView == self.listView) { //点击的是左侧标签
        
        ZCAttentionListModel *model = self.listArray[indexPath.row];
        
        [self contentViewLoadDataWithModel:model];
//        [self.contentView.mj_header beginRefreshing];
        
    }else{//点击的是右侧标签
        
        ZCLog(@"点击了右侧的cell");
    }
}


@end
